package com.coolwen.service.expertAndCouncilor;

import com.coolwen.dao.ExpertAndCouncilorMapper;
import com.coolwen.entity.ExpertAndCouncilor;
import com.coolwen.entity.Pager;
import com.coolwen.entity.SystemContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by nieyu on 2016/8/18.
 */
@Service("expertAndCouncilorService")
public class ExpertAndCouncilorServiceImpl implements ExpertAndCouncilorService{

    @Autowired
    ExpertAndCouncilorMapper expertAndCouncilorMapper;
    @Override
    public int insertSelective(ExpertAndCouncilor eAndC) {
        return expertAndCouncilorMapper.insertSelective(eAndC);
    }

    @Override
    public List<ExpertAndCouncilor> queryAllInfo() {
        return expertAndCouncilorMapper.queryAllInfo();
    }


    @Override
    public Pager<ExpertAndCouncilor> find(String grade, String department, String major) {
        int pageSize = SystemContext.getPageSize();
        int pageOffset = SystemContext.getPageOffset();

        Pager<ExpertAndCouncilor> pages = new Pager<ExpertAndCouncilor>();
        Map<String, Object> params = new HashMap<String, Object>();

        if (grade != null && !grade.equals("")) {
            params.put("grade", "%" + grade + "%");

        }
        if (department != null && !department.equals("")) {
            params.put("department", "%" + department + "%");
        }
        if (major != null && !major.equals("")) {
            params.put("major", "%" + major + "%");
        }
        params.put("pageSize",  pageSize);
        params.put("pageOffset", pageOffset);


        List<ExpertAndCouncilor> datas = expertAndCouncilorMapper.find(params);

        int totalRecord = expertAndCouncilorMapper.find_count(params);
        pages.setDatas(datas);
        pages.setPageOffset(pageOffset);
        pages.setPageSize(pageSize);
        pages.setTotalPage(totalRecord);
        return pages;
    }

    @Override
    public List<ExpertAndCouncilor> findAll(String grade, String department, String major) {
        Map<String, Object> params = new HashMap<String, Object>();

        if (grade != null && !grade.equals("")) {
            params.put("grade", "%" + grade + "%");

        }
        if (department != null && !department.equals("")) {
            params.put("department", "%" + department + "%");
        }
        if (major != null && !major.equals("")) {
            params.put("major", "%" + major + "%");
        }
        List<ExpertAndCouncilor> datas = expertAndCouncilorMapper.find(params);
        return datas;
    }
}
